import pymongo
import requests
import json

Client = pymongo.MongoClient("127.0.0.1",27017)

Collection = Client.mag.trendPageDSubject

Create_URL = "https://wiki.lmd.knogen.com:10443/goapi/storage/create"

Subjects = ["Number theory", "Logic", "Genome editing", "Quantum computing", "Experimental physics", "Deep learning", "Operating system", "Genetic engineering", "Literature", "Geometry", "Industrial engineering", "Applied physics", "Applied mathematics", "Algebra", "Cognitive science", "Theoretical physics", "Philosophy", "Linguistics", "Biological engineering", "Anthropology", "Computer engineering", "Civil engineering", "History", "Theoretical computer science", "Discrete mathematics", "Political science", "Environmental engineering", "Mechanical engineering", "Earth science", "Geography", "Machine learning", "Electrical engineering", "Chemical engineering", "Environmental science", "Sociology", "Neuroscience", "Artificial intelligence", "Geology", "Economics", "Mathematics", "Physics", "Psychology", "Materials science", "Computer science", "Engineering disciplines", "Chemistry", "Biology", "Medicine","empty"]

retData = []
for doc in Collection.find():
    # data = {
    #     "data":doc,
    #     "path":f"Disruption_{doc['_id']}"
    # }
    # ret = requests.post(Create_URL,json=data)
    # print(ret.status_code)

    # 数据直接处理
    retData.append(doc)

# 按照 percent 排序
retData.sort(key=lambda x:x['percent'])

retDataDict = {}

for doc in retData:
    retDataDict.setdefault(doc['subject'],{'y':[],'in':{},'out':{}})
    # 存储 平均颠覆度
    retDataDict[doc['subject']]['y'].append(round(doc['average'],5))

    # 存储，linksin
    all_in_count = 0
    all_out_count = 0
    all_in_list = []
    all_out_list = []
    for sname in Subjects:
        all_out_count += doc['outS'].get(sname,0)
        all_in_count += doc['inS'].get(sname,0)
    for sname in Subjects:
        all_out_list.append( round(doc['outS'].get(sname,0)/all_out_count,5))
        all_in_list.append(round(doc['inS'].get(sname,0)/all_in_count,5))

    # 存储 in out 顺序
    retDataDict[doc['subject']]['in'] = all_in_list
    retDataDict[doc['subject']]['out'] = all_out_list

with open('/tmp/dump.json','wt')as f:
    json.dump({"data":retDataDict,"subject":Subjects},f)